<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" th:href="@{/xadmin/css/font.css}">
    <link rel="stylesheet" th:href="@{/xadmin/css/xadmin.css}">
    <script th:src="@{/xadmin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/xadmin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .layui-table-body {
            overflow-x: hidden;
        }
    </style>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <select name="termId" lay-verify="required" class="show-term-container"></select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" lay-submit="" lay-filter="set">设置</button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table" id="test" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['table', 'form'], function () {
        var table = layui.table;
        var form = layui.form;
        var load = layer.load(1);
        getTerm(function () {
            layui.form.render('select');
            getTable();
            layer.close(load);
        });
        form.on('submit(set)', function (data) {
            // console.log(data.field);
            let termId = data.field['termId'];
            sessionStorage.setItem('termId', termId);
            table.reload('test', {
                url: '/extraTime/time?termId=' + termId
            });
            // console.log("重新渲染完成termId " + sessionStorage.getItem('termId'));
            return false;
        });

        function getTerm(callback) {
            $.ajax({
                url: "/term",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = '';
                    if (result.data && result.data.length > 0) {
                        sessionStorage.setItem("termId", result.data[0].id);
                        let termId = result.data[0].id;
                        // console.log(termId);
                        for (var i in result.data) {
                            // console.log(result.data[i].id);
                            if (result.data[i].id == termId) {
                                // console.log("==");
                                html += '<option value="' + result.data[i].id + '" selected="selected">' + result.data[i].name + '</option>';
                            } else
                                html += '<option value="' + result.data[i].id + '">' + result.data[i].name + '</option>';
                        }
                        $(".show-term-container").html(html);
                    }
                    if (callback) {
                        callback();
                    }
                    // console.log(html)
                }
            });
        }

        function getTable() {
            table.render({
                elem: '#test',
                url: '/extraTime/time?termId=' + sessionStorage.getItem('termId'),
                toolbar: '#toolbarDemo',
                cols: [
                    [
                        {type: 'numbers', width: 80, sort: true, title: '序号'},
                        {field: 'name', minWidth: 120, title: '名称'},
                        {
                            field: 'beginTime',
                            minWidth: 180,
                            templet: '<div>{{layui.util.toDateString(d.beginTime*1, `yyyy-MM-dd HH:mm:ss`)}}</div>',
                            title: '开始时间'
                        },
                        {
                            field: 'endTime',
                            minWidth: 180,
                            templet: '<div>{{layui.util.toDateString(d.endTime*1, `yyyy-MM-dd HH:mm:ss`)}}</div>',
                            title: '结束时间'
                        },
                        {
                            minWidth: 90,
                            title: '状态',
                            templet: function (d) {
                                // console.log(d.beginTime + " " + d.endTime);
                                let nowTime = new Date().getTime();
                                if (d.beginTime > nowTime) {
                                    return "<div><span style='color: red; font-weight: bold;'>未开始</span></div>";
                                } else if (d.endTime < nowTime) {
                                    return "<div>已结束</div>";
                                } else {
                                    return "<div><span style='color: #16b116; font-weight: bold;'>进行中</span></div>";
                                }
                            }
                        },
                        {midWidth: 50, title: '查看', toolbar: '#barView'},
                        {midWidth: 50, title: '删除', toolbar: '#barOptional'}
                    ]
                ],
                page: false
            });
        }

        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除么', function (index) {
                    var load = layer.load(2);
                    $.ajax({
                        url: '/extraTime/time',
                        type: "DELETE",
                        data: {id: data.id},
                        dataType: 'json',
                        success: function (result) {
                            layer.close(load)
                            if (result.code === 500 || result.code === 5000100 || result.code === 5000101 || result.code === 5000102) {
                                layer.alert(result.msg, {icon: 2});
                            } else {
                                layer.alert("删除成功", {icon: 1}, function (index1) {
                                    layer.close(index1);
                                    //xadmin.father_reload();
                                    table.reload('test');
                                });
                            }
                        },
                    });

                });
            } else if (obj.event === 'view') {
                // console.log("view ");
                sessionStorage.setItem('timeId', obj.data.id);
                // window.open('/tutor/getPage?name=extra-score-list');
                // xadmin.open('提交记录', '/tutor/getPage?name=extra-score-list');
                console.log("add tab")
                parent.xadmin.add_tab(obj.data.name + '●学生提交', '/tutor/getPage?name=extra-score-list&tmp=' + obj.data.name);
            }
        });

        table.on('toolbar(test)', function (obj) {
            if (obj.event === 'add') {
                xadmin.open('添加', '/tutor/getPage?name=extra-time-add.html');
            }
        });
    });
</script>

<script type="text/html" id="barOptional">
    <a class="" lay-event="del" title="删除">
        <i class="layui-icon">&#xe640;</i>
    </a>
</script>
<script type="text/html" id="barView">
    <a class="" lay-event="view" title="查看">
        <button type="button" class="layui-btn">查看</button>
    </a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <div class="layui-inline" title="添加" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
    </div>
</script>
</html>